package com.mlz.mlzmall.ui.activity.act.main;


import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.mlz.mlzmall.R;
import com.mlz.mlzmall.ui.activity.frag.main.BrandFragment;
import com.mlz.mlzmall.ui.activity.frag.main.GoodsDisplayFragment;
import com.mlz.mlzmall.ui.activity.frag.main.SpecialFragment;
import com.mlz.mlzmall.ui.adapter.MainPagerAdapter;
import com.mlz.mlzmall.ui.utils.util.AppManager;
import com.mlz.mlzmall.ui.utils.util.LogUtils;


import java.util.ArrayList;

/**
 * Created by jason.zhou on 2016/1/23.
 * copy by pm 2016/5/18
 */
public class TopicActivity extends FragmentActivity  {
    private ArrayList<Fragment> mFragmentList = new ArrayList<Fragment>();
  //  private FragmentAdapter mFragmentAdapter;

    private BrandFragment brandFragment;
    private SpecialFragment specialFragment;
    private GoodsDisplayFragment goodsDisplayFragment;

    /**
     * ViewPager的当前选中页
     */
 //   private int currentIndex;

    /**
     * 屏幕的宽度
     */
 //   private int screenWidth;

    boolean[] fragmentsUpdateFlag = { false, false, false, false };
    private TextView tv_title;
    private RelativeLayout rl_back;
    private RadioGroup rg_choose;
    private ViewPager viewPager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic);
     //   AppManager.getAppManager().addActivity(this);
        init();
        if(getIntent().getIntExtra("position",0)==0) {
            initView(0);
        }else if(getIntent().getIntExtra("position",0)==1)
        {
            initView(1);
        }else
        {
            initView(2);
        }
    }
    public void init()
    {
        brandFragment = new BrandFragment();
        specialFragment = new SpecialFragment();
        goodsDisplayFragment = new GoodsDisplayFragment();


        mFragmentList.add(brandFragment);
        mFragmentList.add(specialFragment);
        mFragmentList.add(goodsDisplayFragment);
    }
    private void initView(int position) {
        rl_back = (RelativeLayout) findViewById(R.id.iv_back_topics);
        tv_title = (TextView) findViewById(R.id.tv_topic_titles);
        rg_choose = (RadioGroup) findViewById(R.id.rg_content_fragment);
        viewPager = (ViewPager) findViewById(R.id.vp);
        tv_title.setText("Ariul");
      //  viewPager.setOffscreenPageLimit(0);
        rgCheck(position);
//        mFragmentAdapter = new FragmentAdapter(TopicActivity.this.getSupportFragmentManager(), mFragmentList);
//        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setAdapter(new MainPagerAdapter(mFragmentList,getSupportFragmentManager()));
       // viewPager.setAdapter(new MyFragmentPagerAdapter(TopicActivity.this.getSupportFragmentManager()));
     //   viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager(), mFragmentList));
        viewPager.setCurrentItem(position);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rgCheck(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg_choose.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               fragmentCheck(checkedId);
            }
        });
        rl_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d("触发了");
                finish();

            }
        });
    }
    public void rgCheck(int position) {
        switch (position) {
            case 0:
                rg_choose.check(R.id.btn_introduce_goods);
                break;
            case 1:
                rg_choose.check(R.id.btn_topic_activity);
                break;
            case 2:
                rg_choose.check(R.id.btn_goods_show);
                break;
            default:
                break;
        }
    }
    public void fragmentCheck(int id)
    {
        switch (id)
        {
            case R.id.btn_introduce_goods:
                viewPager.setCurrentItem(0);
                break;
            case R.id.btn_topic_activity:
                viewPager.setCurrentItem(1);
                break;
            case R.id.btn_goods_show:
                viewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }

}

